package com.aliengen.thingple.update;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Environment;

public class HttpDownloadUtil {
	public String downFile(String urlstr){
		System.out.println(urlstr);
		StringBuffer sb = new StringBuffer();
		BufferedReader buffer = null;
		String line = null;
		try {
			buffer=new BufferedReader(new InputStreamReader(getInputStreamFromUrl(urlstr)));
			System.out.println(urlstr + "2");
			while((line=buffer.readLine())!=null){
				sb.append(line);
				System.out.println(line);
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				buffer.close();
			}catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
    public int download(String urlStr, String path, String fileName) {  
        InputStream inputStream = null;  
        FileUtil fileUtils = new FileUtil();
        try {              
            if (fileUtils.isFileExist(path + fileName)) {  
                return 1;  
            } else {  
                inputStream = getInputStreamFromUrl(urlStr);  
                File resultFile = fileUtils.writeToSDCard(path, fileName,  
                        inputStream);  
                if (resultFile == null) {  
                	fileUtils.deleteFile(path+fileName);
                    return 2;  
                }  
            }  
        } catch (Exception e) {  
            e.printStackTrace();
            fileUtils.deleteFile(path+fileName);
            return 3;  
        } finally {  
            try {  
                if (inputStream != null){
                	inputStream.close();
                	
                }
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        return 4;  
    }  	
    public InputStream getInputStreamFromUrl(String urlStr) {  
        InputStream inputStream = null;  
        try {  
            URL url = new URL(urlStr);  
            HttpURLConnection urlConn = (HttpURLConnection) url  
                    .openConnection();  
            inputStream = urlConn.getInputStream();  
        } catch (MalformedURLException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return inputStream;  
    } 
    public int getBinFile(byte[] data){
    	String mFilePath = Environment.getExternalStorageDirectory() + "/CMC165M.bin";
    	int length  = 0;
    	try {
			FileInputStream mFileInputStream = new FileInputStream(mFilePath);
			length = mFileInputStream.available();
			mFileInputStream.read(data);
			mFileInputStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block			
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return length;
    }
    public boolean deleteFile(){
    	String mFilePath = Environment.getExternalStorageDirectory() + "/CMC165M.bin";
    	File mFile = new File(mFilePath);
    	if (mFile.isFile() && mFile.exists()){
    		return mFile.delete();
    	}
    	return false;
    }
}
